<template xmlns:el-col="http://www.w3.org/1999/html">
    <div>
<!--        <div class="crumbs">-->
<!--            <el-breadcrumb separator="/">-->
<!--                <el-breadcrumb-item>-->
<!--                    <i class="el-icon-lx-cascades"></i> 项目列表-->
<!--                </el-breadcrumb-item>-->
<!--            </el-breadcrumb>-->
<!--        </div>-->
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.projectNm" placeholder="项目名称" class="handle-input mr10" @keyup.enter.native="handleSearch"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="el-button" icon="el-icon-delete" @click="handleReset">重置</el-button>
                <div class="right-items" style="float: right;" >
                    <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
                </div>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
            >
                <el-table-column prop="projectId" label="项目ID" width="200" align="center"></el-table-column>
                <el-table-column prop="projectNm" label="项目名称" align="center" width="300"></el-table-column>
                <el-table-column prop="projectEnglishNm" label="项目英文名称" align="center"></el-table-column>
                <el-table-column prop="proPeople" label="下位机对接人" align="center"></el-table-column>
                <el-table-column prop="projectDate" label="项目上线日期"  align="center"></el-table-column>
                <el-table-column prop="istTm" label="插入时间"  align="center"></el-table-column>
                <el-table-column  label="操作" width="300" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-sort"
                                @click="addVersion(scope.$index, scope.row)"
                        >上传版本</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 上传版本弹出框 -->
        <el-dialog title="上传版本" :visible.sync="addVersionVisible" width="40%" :before-close="addVersionColse">
            <el-form ref="addVersionform" :model="addVersionform" label-width="120px" :rules="addVersionRules">
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="上传类型" prop="type" >
                            <el-select v-model="addVersionform.type" placeholder="请选择上传类型" style="width: 80%">
                                <el-option key="00" label="服务端" value="00"></el-option>
                                <el-option key="01" label="客户端" value="01"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="版本号" prop="version" >
                            <el-input  v-model="addVersionform.version" style="width: 80%"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="20">
                        <el-form-item label="版本文件" prop="upLoadFile" >
                            <el-upload
                                    class="upload-demo"
                                    drag
                                    multiple
                                    :limit="1"
                                    :on-remove="handleRemove"
                                    :on-exceed="handleExceed"
                                    action=""
                                    :http-request="handlePost"
                                    accept=".zip,.rar"
                            :file-list="fileList">
                                <i class="el-icon-upload"></i>
                                <div class="el-upload__text">将上传的压缩文件拖到此处，或<em>点击上传</em></div>
                            </el-upload>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="20">
                        <el-form-item label="说明" prop="rmk" >
                            <el-input type="textarea"  v-model="addVersionform.rmk" style="width: 100%;font-size: 16px;"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVersionColse">取 消</el-button>
                <el-button type="primary" @click="saveAddVersion">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="40%" :before-close="editColse">
            <el-form ref="form" :model="form" :rules="addFormRules" label-width="120px" >
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="项目名称" prop="projectNm" >
                            <el-input  v-model="form.projectNm" style="width: 100%"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="项目英文名称" prop="projectEnglishNm" >
                            <el-input  v-model="form.projectEnglishNm" style="width: 100%"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="下位机对接人" prop="proPeople">
                            <el-input v-model="form.proPeople" style="width: 100%;"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="研发开始日期" prop="projectDate" >
                            <el-date-picker style="width: 100%;"
                                            v-model="form.projectDate"
                                            type="date"
                                            placeholder="选择日期"
                                            value-format="yyyy-MM-dd"
                                            :picker-options="projectOptions">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="20">
                        <el-form-item label="备注" prop="remarks">
                            <el-input   type="textarea"  v-model="form.remarks" style="width: 100%;"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editColse">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 新增弹出框 -->
        <el-dialog title="新增" :visible.sync="addVisible" width="40%" :before-close="closeAdd">
            <el-form ref="addForm" :rules="addFormRules" :model="addForm" label-width="120px">
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="项目名称" prop="projectNm" >
                            <el-input v-model="addForm.projectNm" style="width: 100%"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="项目英文名称" prop="projectEnglishNm">
                            <el-input v-model="addForm.projectEnglishNm" style="width: 100%;"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="下位机对接人" prop="proPeople">
                            <el-input v-model="addForm.proPeople" style="width: 100%;"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="研发开始日期" prop="projectDate" >
                            <el-date-picker style="width: 100%;"
                                            v-model="addForm.projectDate"
                                            type="date"
                                            placeholder="选择日期"
                                            value-format="yyyy-MM-dd"
                                            :picker-options="projectOptions">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="20">
                        <el-form-item label="备注" prop="remarks">
                            <el-input   type="textarea"  v-model="addForm.remarks" style="width: 100%;"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click='closeAdd'>取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import {projectlist, selectProById,doAdd,doUpdate,doDelete,addVersion} from '../../../api/project/project';
    export default {
        name: 'project',
        data() {
            return {
                query: {//检索条件
                    projectNm: '',
                    userAcct:localStorage.getItem('ms_userAcct'),
                    pageIndex: 1,
                    pageSize: 10
                },
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                pageTotal: 0,
                form: {},
                addVisible: false,
                addForm:{},
                addVersionform:{},
                file:File,
                fileSize:0,
                addVersionVisible:false,
                projectId:"",
                fileList:[],
                addFormRules: {
                    projectNm: [
                        { required: true, message: '请输入项目名称', trigger: 'blur' },
                    ],
                    projectEnglishNm: [
                        { required: true, message: '请输入项目英文名称', trigger: 'blur' },
                    ],
                },
                addVersionRules:{
                    projectNm: [
                        { required: true, message: '请输入项目名称', trigger: 'blur' },
                    ],
                    projectEnglishNm: [
                        { required: true, message: '请输入项目名称', trigger: 'blur' },
                    ],
                    type: [
                        { required: true, message: '请输入项目名称', trigger: 'blur' },
                    ],
                    version: [
                        { required: true, message: '请输入项目名称', trigger: 'blur' },
                    ]
                },
                projectOptions:{
                    disabledDate(time) {
                        return time.getTime() > Date.now() - 8.64e6;
                    }
                },
            };
        },
        created() {
            this.getData();
        },
        methods: {
            // 获取 easy-mock 的模拟数据
            getData() {
                projectlist(this.query).then(res => {
                    this.tableData = res.data;
                    this.pageTotal = res.total;
                });
            },
            // 触发搜索按钮
            handleSearch() {
                this.$set(this.query, 'pageIndex', 1);
                this.getData();
            },
            // 触发重置按钮
            handleReset() {
                this.query.projectNm = "";
                this.getData();
            },
            // 新增操作
            handleAdd() {
                this.addVisible = true;
            },
            /*新增项目*/
            saveAdd(){
                // validate 函数 表单验证
                this.$refs['addForm'].validate((valid) => {
                    if (valid) {
                        //新增form表单提交到后台
                        doAdd(this.addForm).then(res => {
                            if (res.success==true && res.data==1){
                                this.addVisible=false;
                                this.addForm={};
                                this.getData();
                                this.$message.success('保存成功');
                            }else{
                                this.$message.error('保存失败');
                            }
                        });
                    }
                });
            },
            closeAdd(){//关闭新增弹出框
                    this.$confirm('确认关闭？', '提示', {
                        type: 'warning'
                    })
                        .then(() => {
                            this.addVisible=false;
                            this.addForm={};
                            this.getData();
                        })
                        .catch(() => {
                            this.$message({
                                message:'已取消操作'
                            });
                        });
            },
            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        //后台进行删除操作
                        doDelete({"projectId":row.projectId}).then(res => {
                            if (res.success==true && res.data==1){
                                this.$message.success('删除成功');
                                this.getData();
                            }else {
                                this.$message.error('删除失败');
                                this.getData();
                            }
                        });
                    })
                    .catch(() => {
                        this.$message({
                            message:'已取消删除'
                        });
                        this.getData();
                    });
            },
            // 编辑操作
            async handleEdit(index, row) {
                this.idx = index;
                await selectProById({projectId:row.projectId}).then(res =>{
                    if (res.success==true){
                        this.form=res.data;
                        this.editVisible = true;
                    }else{
                        this.$message.error('查询项目失败');
                    }
                });
            },
            // 保存编辑
            saveEdit() {
                // validate 函数 表单验证
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        //修改后的数据发送到后台
                        doUpdate(this.form).then(res => {
                            if (res.success==true && res.data == 1) {
                                this.editVisible = false;
                                this.form = {};
                                this.getData();
                                this.$message.success('编辑成功');
                            } else {
                                this.$message.error('编辑失败');
                            }
                        });
                    }
                })
            },
            editColse(){//关闭编辑弹出框
                this.$confirm('确认关闭？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        this.editVisible=false;
                        this.form={};
                        this.getData();
                    })
                    .catch(() => {
                        this.$message({
                            message:'已取消操作'
                        });
                    });

            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            },
            //上传版本
            addVersion(index, row){
                this.projectId=row.projectId;
                this.addVersionform={};
                this.fileSize=0;
                this.addVersionVisible=true;
            },
            addVersionColse(){
                this.addVersionVisible=false;
                this.fileList=[];
            },
            handlePost(eitem){
                this.fileSize=this.fileSize+1;
                if (this.fileSize==1){
                    this.file=eitem.file;
                }else{
                    this.$message.warning("当前只能上传一个文件");
                }
            },
            handleExceed(){
                this.$message.warning("当前只能上传一个文件");
            },
            handleRemove(){
                this.fileSize=this.fileSize-1;
            },
            saveAddVersion(){
                if (this.fileSize!==1){
                    this.$message.error('请上传版本文件');
                    return;
                };
                let extension = this.file.name.split(".");
                if (extension[extension.length-1] !=="zip" && extension[extension.length-1] !=="rar"){
                    this.$message.error('请上传zip或者rar类型的版本文件');
                    return;
                };
                // validate 函数 表单验证
                this.$refs['addVersionform'].validate((valid) => {
                    if (valid) {
                        //上传项目版本
                        let formData = new FormData();
                        formData.append('upLoadFile', this.file);//filename是键，file是值，就是要传的文件，test.zip是要传的文件名
                        formData.append("projectId", this.projectId);
                        formData.append("type", this.addVersionform.type);
                        formData.append("version", this.addVersionform.version);
                        formData.append("rmk", this.addVersionform.rmk);
                        const loading = this.$loading({
                            lock: true,
                            text: '上传中',
                            spinner: 'el-icon-loading',
                            background: 'rgba(0, 0, 0, 0.7)'
                        });
                        //上传项目版本
                        addVersion(formData).then(res => {
                            loading.close();
                            if (res.success==true){
                                this.addVersionVisible = false;
                                this.addVersionform = {};
                                this.getData();
                                this.$message.success('上传版本成功');
                            }else{
                                this.$message.error('上传版本失败');
                            }
                        });
                    }
                });
            }
        }
    };
</script>
<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .schart {
        width: 550px;
        height: 300px;
    }
    .content-title {
        clear: both;
        font-weight: 400;
        line-height: 50px;
        margin: 10px 0;
        font-size: 22px;
        color: #1f2f3d;
    }
</style>
